#pragma once

#include <iostream>
#include <cstring>
#include <string>
#include <cerrno>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include <functional>

#include "Log.hpp"
#include "Common.hpp"
#include "InetAddr.hpp"
#include "ThreadPool.hpp"

#define BACKLOG 8

using namespace LogModule;
using namespace ThreadModule;

static const uint16_t gport = 8082;
using handler_t = std::function<std::string(std::string)>;

class TcpServer
{
    using task_t = std::function<void()>;
    struct ThreadData
    {
        int sockfd;
        TcpServer *self;
    };

public:
    TcpServer(handler_t handler , int port = gport):
    _handler(handler) , _port(port) , _isrunning(false)
    {

    }
    void InitServer()
    {
        //1.创建tcp socket
        _listensockfd = ::socket(AF_INET , SOCK_STREAM,0);
        if(_listensockfd < 0)
        {
            LOG(LogLevel::FATAL) << "socket error";
            Die(SOCKET_ERR);
        }
        LOG(LogLevel::INFO) << "socket create success , sockfd is : " << _listensockfd;

        int opt = 1;
        setsockopt(_listensockfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof (opt));

        struct sockaddr_in local;
        memset(&local, 0, sizeof((local)));
        local.sin_family = AF_INET;
        local.sin_port = htons(gport);
        local.sin_addr.s_addr = INADDR_ANY;

        //2.bind
        int n = ::bind(_listensockfd, CONV(&local), sizeof(local));
        if(n < 0)
        {
            LOG(LogLevel::FATAL) << "bind error";
            Die(BIND_ERR);
        }
        LOG(LogLevel::INFO) << "bind success, sockfd is :" << _listensockfd;

        //3.cs tcp是面向连接的 要求tcp随时随地等待被连接
        //tcp需要将socket设置成监听状态
        n = ::listen(_listensockfd, BACKLOG);
        if(n < 0)
        {
            LOG(LogLevel::FATAL) << "listen error";
            Die(LISTEN_ERR);
        }
        LOG(LogLevel::INFO) << "listen success, sockfd id :" << _listensockfd;
    }
    // void HandlerRequest(int sockfd)
    // {

    // }
    // static void *ThreadEntry(void *args)
    // {

    // }
    void Start()
    {
        _isrunning = true;
        while(_isrunning)
        {
            //不能直接读取数据
            //1.获取新连接
            struct sockaddr_in peer;
            socklen_t peerlen = sizeof(peer);
            LOG(LogLevel::DEBUG) << "accept ing ...";
            //我们要获取client的信息
            int sockfd = ::accept(_listensockfd, CONV(&peer), &peerlen);
            if(sockfd < 0)
            {
                LOG(LogLevel::WARNING) << "accept error :" << strerror(errno);
                continue;
            }
            //获取连接成功了
            LOG(LogLevel::INFO) << "accept success, sockfd is :" << sockfd;
            InetAddr addr(peer);
            LOG(LogLevel::INFO) << "client info : " << addr.Addr();
            
        }
    }
    void Stop()
    {
        _isrunning = false;
    }
    ~TcpServer()
    {}
    
private:
    int _listensockfd;//监听socket
    uint16_t _port;
    bool _isrunning;

    handler_t _handler;//处理上层任务的入口
};